//
//  ComposedViewController.m
//  Weibo
//
//  Created by wpapple on 16/6/3.
//  Copyright © 2016年 wpapple. All rights reserved.
//

#import "ComposedViewController.h"
#import "WPTextView.h"
#import "PicCollectionViewController.h"

@interface ComposedViewController ()<UITextViewDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic,strong) UIImagePickerController *imageVc;

@property (nonatomic,strong) PicCollectionViewController *PicVc;

@end

@implementation ComposedViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNav];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self setTextViewStyle];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(selectdPicture:) name:KweiboAddPicture object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(deletePicture:) name:KweiboCancelPicture object:nil];
}

-(void)selectdPicture:(NSNotification *)notification
{
    UIImagePickerController *imageVc = [[UIImagePickerController alloc]init];
    
    self.imageVc = imageVc;
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        
        imageVc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }else
    {
        NSLog(@"请打开相册");
    }
    
    imageVc.delegate = self;
    
    [self presentViewController:imageVc animated:YES completion:nil];
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo NS_DEPRECATED_IOS(2_0, 3_0)
{
    
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    
    [self.PicVc.ImageArray addObject:image];
    [self.PicVc.collectionView reloadData];
    
    [self.imageVc dismissViewControllerAnimated:YES completion:nil];
    
}

-(void)deletePicture:(NSNotification *)notification
{
    UIImage *image = notification.userInfo[@"images"];
    
    [self.PicVc.ImageArray removeObject:image];
    [self.PicVc.collectionView reloadData];
    
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)setTextViewStyle
{
    self.textView.delegate = self;
    
    self.textView.font = [UIFont systemFontOfSize:15.0];
}

-(void)setNav
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(close)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(sender)];
}

- (void)close {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)sender
{
    if (self.PicVc.ImageArray.count > 0) {
        [self sendStatusWithPic];
    }else
    {
        [self sendStatus];
    }
    
}

-(void)sendStatusWithPic
{
    NSString *url = @"https://upload.api.weibo.com/2/statuses/upload.json";
    
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    
    WPAccount *account = [WPAccount accountFromSandbox];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"access_token"] = account.access_token;
    parameters[@"status"] = self.textView.text;
    
    [manger POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        UIImage *image = [self.PicVc.ImageArray lastObject];
        
        NSData *data = UIImagePNGRepresentation(image);
        
        [formData appendPartWithFileData:data name:@"pic" fileName:@"pic.png" mimeType:@"image/png"];
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
}

-(void)sendStatus
{
    NSString *url = @"https://api.weibo.com/2/statuses/update.json";
    
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    
    WPAccount *account = [WPAccount accountFromSandbox];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"access_token"] = account.access_token;
    parameters[@"status"] = self.textView.text;
    
    [manger POST:url parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        [self dismissViewControllerAnimated:YES completion:nil];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }];

}

#pragma mark - scrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.destinationViewController isKindOfClass:[PicCollectionViewController class]]) {
        self.PicVc = segue.destinationViewController;
    }
    
    
}
@end
